<template>
	<view class="container">
		
		<view class='header'>
			<view class="icon iconfont icon-logo-wechat"></view>
		</view>

		<view class='content'>
			<view>申请获取以下权限</view>
			<text>获得你的公开信息(昵称，头像等)</text>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<template v-if="status == 0">
			<button class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" @getuserinfo="wxLogin">
				授权登录
			</button>
		</template>
		
		<template v-else-if="status == 1">
			<button class='bottom' type='primary' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
				获取手机号
			</button>
		</template>
		
		<template v-else>
			<button class='bottom' type='primary' @click="toIndex">
				返回
			</button>
		</template>
		<!-- #endif -->
	</view>
</template>

<script>
	import Storage from '@/common/scripts/storage.js'
	import config from '@/common/scripts/config.js'
	import IndexService from './service/service.js'
	import WXBizDataCrypt from '@/static/scripts/WXBizDataCrypt.js'

	export default {
		beforeCreate() {
			this.service = new IndexService()
		},
		name: 'index-login',
		data() {
			return {
				logining: false,
				appid: config.appid,
				secret: config.secret,
				openURL: config.openURL,
				provider: config.provider,
				access_token: null,
				sessionKey: null,
				status: 0,
				openID: null,
				phone: null,
				unionID: null,
				code: null,
				userInfo: null,
				encryptedData: null,
				iv: null,
				newCode: null,
				options: null
			}
		},
		onLoad(options) {
			let that = this
			console.log(options)
			Storage.set('back-page', options)
		},
		methods: {
			toClear() {
				this.service.clear()
			},
			toIndex() {
				let myIndex = { g: "Wap", c: "Mall", a: "home", lj_page: "/wap.php" }
				Storage.set('back-page', myIndex)
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			toMyCenter() {
				let myCenter = { g: "Wap", c: "Mall", a: "myCenter", lj_page: "/wap.php" }
				Storage.set('back-page', myCenter)
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			init() {
				let that = this
				// #ifdef MP-WEIXIN
				that.status = 0
				that.initWx()
				// #endif
			},
			initWx() {
				let that = this
				this.service.login({})
					.then(that.loginSuccess).catch(that.loginError)
					.then(that.getUserInfoSuccess).catch(that.getUserInfoError)
					.then(that.getNewCodeSuccess).catch(that.getNewCodeError)
					.then(that.getOpenIDPhpSuccess).catch(that.getOpenIDPhpError)
					// .then(that.getInfoSuccess).catch(that.getInfoError)
			},
			getOpenIDPhpSuccess(result) {
				let that = this
				console.log(result)
				if (result) {
					this.status = 1
					this.openID = result.openid
					this.unionID = result.unionid
					this.sessionKey = result.session_key
					return {}
				}
				return null
			},
			getOpenIDPhpError(err) {
				console.log(err)
				if (err) {
					this.status = 1
					return null
				}
			},
			loginSuccess(result) {
				console.log(result)
				if (result) {
					this.status = 0
					this.code = result.code
					let options = {
						code: result.code
					}
					return this.service.getUserInfo()
				} else {
					return null
				}
			},
			loginError(err) {
				console.log(err)
				if (err) {
					this.status = 0
					return null
				}
			},
			getNewCodeSuccess(result) {
				console.log(result)
				if (result) {
					let options = {
						code: result.code
					}
					return this.service.getOpenIDPhp(options)
				} else {
					return null
				}
			},
			getNewCodeError(err) {
				console.log(err)
				if (err) {
					this.status = 0
					return null
				}
			},
			getUserInfoSuccess(result) {
				console.log(result)
				let that = this
				if (result) {
					that.status = 1
					that.userInfo = result.userInfo
					that.encryptedData = result.encryptedData
					that.iv = result.iv
					return that.service.login({})
				} else {
					this.status = 0
					return null
				}
			},
			getUserInfoError(err) {
				console.log(err)
				if (err) {
					// this.status = 0
					return null
				}
			},
			getOpenIDSuccess(result) {
				let that = this
				console.log(result)
				if (result) {
					this.status = 1
					this.openID = result.openid
					this.unionID = result.unionid
					this.sessionKey = result.session_key
					return {}
				}
				return null
			},
			getOpenIDError(err) {
				console.log(err)
				if (err) {
					this.status = 1
					return null
				}
			},
			getCodeSuccess(data) {
				let that = this
				console.log(data)
				console.log(that.code, that.newCode)
				if (data) {
					this.newCode = data.code
					let options = {
						code: that.newCode,
						encryptedData: that.encryptedData,
						iv: that.iv,
						purePhoneNumber: that.options.purePhoneNumber,
						countryCode: that.options.countryCode
					}
					console.log(options)
					return this.service.getInfo(options)
				} else {
					return null
				}
			},
			getCodeError(err) {
				console.log(err)
				return null
			},
			getInfoSuccess(data) {
				let that = this
				console.log(data)
				if (data) {
					let isHasTicket = Storage.has('ticket')
					if (!isHasTicket && data.result.hasOwnProperty('ticket') && data.result.ticket) {
						Storage.set('ticket', data.result.ticket)
					}
					let isHasUser = Storage.has('user')
					if (!isHasUser && data.result.hasOwnProperty('user') && data.result.user) {
						Storage.set('user', data.result.user)
					}
					that.toIndex()
				} else {
					return null
				}
			},
			getInfoError(err) {
				console.log(err)
				return null
			},
			getPhoneNumber(e) { //这个事件同样需要拿到e
				let that = this
				let ency = e.detail.encryptedData;
				let iv = e.detail.iv;
				let errMsg = e.detail.errMsg
				
				let pc = new WXBizDataCrypt(config.appid, that.sessionKey)

				let data = pc.decryptData(that.encryptedData, that.iv)
				let phoneData = pc.decryptData(ency, iv)
				
				console.log(data, phoneData)
				
				let options = {
					code: that.newCode,
					encryptedData: that.encryptedData,
					iv: that.iv,
					purePhoneNumber: phoneData.purePhoneNumber,
					countryCode: phoneData.countryCode
				}
				
				console.log(options)
				
				that.options = options
				
				console.log(that.newCode)
				
				return this.service.login({})
					.then(that.getCodeSuccess).catch(that.getCodeError)
					.then(that.getInfoSuccess).catch(that.getInfoError)
				// this.service.getInfo(options)
				// 	.then(that.getInfoSuccess).catch(that.getInfoError)
			},
			wxLogin(e) {
				let that = this;
				that.logining = true;
				that.userInfo = e.detail.userInfo
				console.log(that.userInfo)
				uni.login({
					provider: 'weixin',
					success: (login_res => {
						that.code = login_res.code
						that.status = 1
						that.service.getUserInfo()
							.then(that.getUserInfoSuccess).catch(that.getUserInfoError)
							.then(that.getOpenIDPhpSuccess).catch(that.getOpenIDPhpError)
					}),
					fail: function(err) {
						that.status = 0
					}
				})
			}
		},
		created() {
			this.init()
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		overflow: hidden;
		background: #fff;
		flex-direction: column;
		justify-content: center;
		height: 100vh;

		.icon-logo-wechat {
			font-size: 200upx;
			width: 200upx;
			height: 200upx;
			display: inline-block;
		}

		.demo {
			width: 200px;
			height: 200px;
		}

		.confirm-btn {
			width: 630upx;
			height: 76upx;
			line-height: 76upx;
			border-radius: 50px;
			margin-top: 70upx;
			background: #000000;
			color: #fff;
			font-size: 16upx;

			&:after {
				border-radius: 100px;
			}
		}
	}

	.header {
		margin: 90upx 90upx 90upx 50upx;
		border-bottom: 1upx solid #ccc;
		text-align: center;
		width: 650upx;
		height: 300upx;
	}

	.content {
		margin-left: 50upx;
		margin-bottom: 90upx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40upx;
	}

	.bottom {
		border-radius: 80upx;
		margin: 70upx 50upx;
		font-size: 35upx;
	}
</style>
